<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=9"/>
<meta name="generator" content="Doxygen 1.8.8"/>
<title>AndBase开发框架: com.google.gson.TypeAdapterFactory接口 参考</title>
<link href="tabs.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="dynsections.js"></script>
<link href="search/search.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="search/search.js"></script>
<script type="text/javascript">
  $(document).ready(function() { searchBox.OnSelectItem(0); });
</script>
<link href="doxygen.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div id="top"><!-- do not remove this div, it is closed by doxygen! -->
<div id="titlearea">
<table cellspacing="0" cellpadding="0">
 <tbody>
 <tr style="height: 56px;">
  <td id="projectlogo"><img alt="Logo" src="ic_launcher.png"/></td>
  <td style="padding-left: 0.5em;">
   <div id="projectname">AndBase开发框架
   &#160;<span id="projectnumber">1.6</span>
   </div>
  </td>
 </tr>
 </tbody>
</table>
</div>
<!-- end header part -->
<!-- 制作者 Doxygen 1.8.8 -->
<script type="text/javascript">
var searchBox = new SearchBox("searchBox", "search",false,'搜索');
</script>
  <div id="navrow1" class="tabs">
    <ul class="tablist">
      <li><a href="index.html"><span>首页</span></a></li>
      <li><a href="pages.html"><span>相关页面</span></a></li>
      <li><a href="namespaces.html"><span>命名空间</span></a></li>
      <li class="current"><a href="annotated.html"><span>类</span></a></li>
      <li><a href="files.html"><span>文件</span></a></li>
      <li>
        <div id="MSearchBox" class="MSearchBoxInactive">
        <span class="left">
          <img id="MSearchSelect" src="search/mag_sel.png"
               onmouseover="return searchBox.OnSearchSelectShow()"
               onmouseout="return searchBox.OnSearchSelectHide()"
               alt=""/>
          <input type="text" id="MSearchField" value="搜索" accesskey="S"
               onfocus="searchBox.OnSearchFieldFocus(true)" 
               onblur="searchBox.OnSearchFieldFocus(false)" 
               onkeyup="searchBox.OnSearchFieldChange(event)"/>
          </span><span class="right">
            <a id="MSearchClose" href="javascript:searchBox.CloseResultsWindow()"><img id="MSearchCloseImg" border="0" src="search/close.png" alt=""/></a>
          </span>
        </div>
      </li>
    </ul>
  </div>
  <div id="navrow2" class="tabs2">
    <ul class="tablist">
      <li><a href="annotated.html"><span>类列表</span></a></li>
      <li><a href="classes.html"><span>类索引</span></a></li>
      <li><a href="hierarchy.html"><span>类继承关系</span></a></li>
      <li><a href="functions.html"><span>类成员</span></a></li>
    </ul>
  </div>
<!-- window showing the filter options -->
<div id="MSearchSelectWindow"
     onmouseover="return searchBox.OnSearchSelectShow()"
     onmouseout="return searchBox.OnSearchSelectHide()"
     onkeydown="return searchBox.OnSearchSelectKey(event)">
<a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(0)"><span class="SelectionMark">&#160;</span>全部</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(1)"><span class="SelectionMark">&#160;</span>类</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(2)"><span class="SelectionMark">&#160;</span>命名空间</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(3)"><span class="SelectionMark">&#160;</span>文件</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(4)"><span class="SelectionMark">&#160;</span>函数</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(5)"><span class="SelectionMark">&#160;</span>变量</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(6)"><span class="SelectionMark">&#160;</span>枚举值</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(7)"><span class="SelectionMark">&#160;</span>页</a></div>

<!-- iframe showing the search results (closed by default) -->
<div id="MSearchResultsWindow">
<iframe src="javascript:void(0)" frameborder="0" 
        name="MSearchResults" id="MSearchResults">
</iframe>
</div>

<div id="nav-path" class="navpath">
  <ul>
<li class="navelem"><a class="el" href="namespacecom.html">com</a></li><li class="navelem"><a class="el" href="namespacecom_1_1google.html">google</a></li><li class="navelem"><a class="el" href="namespacecom_1_1google_1_1gson.html">gson</a></li><li class="navelem"><a class="el" href="interfacecom_1_1google_1_1gson_1_1_type_adapter_factory.html">TypeAdapterFactory</a></li>  </ul>
</div>
</div><!-- top -->
<div class="header">
  <div class="summary">
<a href="#pub-methods">Public 成员函数</a> &#124;
<a href="interfacecom_1_1google_1_1gson_1_1_type_adapter_factory-members.html">所有成员列表</a>  </div>
  <div class="headertitle">
<div class="title">com.google.gson.TypeAdapterFactory接口 参考</div>  </div>
</div><!--header-->
<div class="contents">
<div class="dynheader">
类 com.google.gson.TypeAdapterFactory 继承关系图:</div>
<div class="dyncontent">
 <div class="center">
  <img src="interfacecom_1_1google_1_1gson_1_1_type_adapter_factory.png" usemap="#com.google.gson.TypeAdapterFactory_map" alt=""/>
  <map id="com.google.gson.TypeAdapterFactory_map" name="com.google.gson.TypeAdapterFactory_map">
<area href="classcom_1_1google_1_1gson_1_1internal_1_1bind_1_1_collection_type_adapter_factory.html" alt="com.google.gson.internal.bind.CollectionTypeAdapterFactory" shape="rect" coords="0,56,359,80"/>
<area href="classcom_1_1google_1_1gson_1_1internal_1_1bind_1_1_map_type_adapter_factory.html" alt="com.google.gson.internal.bind.MapTypeAdapterFactory" shape="rect" coords="369,56,728,80"/>
<area href="classcom_1_1google_1_1gson_1_1internal_1_1bind_1_1_reflective_type_adapter_factory.html" alt="com.google.gson.internal.bind.ReflectiveTypeAdapterFactory" shape="rect" coords="738,56,1097,80"/>
<area href="classcom_1_1google_1_1gson_1_1internal_1_1_excluder.html" alt="com.google.gson.internal.Excluder" shape="rect" coords="1107,56,1466,80"/>
</map>
 </div></div>
<table class="memberdecls">
<tr class="heading"><td colspan="2"><h2 class="groupheader"><a name="pub-methods"></a>
Public 成员函数</h2></td></tr>
<tr class="memitem:a8f4c55ec13b384a3e1cba3c3a827ee48"><td class="memItemLeft" align="right" valign="top">&lt; T &gt; TypeAdapter&lt; T &gt;&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="interfacecom_1_1google_1_1gson_1_1_type_adapter_factory.html#a8f4c55ec13b384a3e1cba3c3a827ee48">create</a> (<a class="el" href="classcom_1_1google_1_1gson_1_1_gson.html">Gson</a> gson, TypeToken&lt; T &gt; type)</td></tr>
<tr class="separator:a8f4c55ec13b384a3e1cba3c3a827ee48"><td class="memSeparator" colspan="2">&#160;</td></tr>
</table>
<a name="details" id="details"></a><h2 class="groupheader">详细描述</h2>
<div class="textblock"><p>Creates type adapters for set of related types. Type adapter factories are most useful when several types share similar structure in their JSON form.</p>
<h3>Example: Converting enums to lowercase</h3>
<p>In this example, we implement a factory that creates type adapters for all enums. The type adapters will write enums in lowercase, despite the fact that they're defined in</p><div class="fragment"><div class="line">CONSTANT_CASE </div>
</div><!-- fragment --><p> in the corresponding Java model: </p><pre><div class="fragment"><div class="line"><span class="keyword">public</span> <span class="keyword">class </span>LowercaseEnumTypeAdapterFactory <span class="keyword">implements</span> TypeAdapter.Factory {</div>
<div class="line">  public &lt;T&gt; TypeAdapter&lt;T&gt; <a class="code" href="interfacecom_1_1google_1_1gson_1_1_type_adapter_factory.html#a8f4c55ec13b384a3e1cba3c3a827ee48">create</a>(Gson gson, TypeToken&lt;T&gt; type) {</div>
<div class="line">    Class&lt;T&gt; rawType = (Class&lt;T&gt;) type.getRawType();</div>
<div class="line">    <span class="keywordflow">if</span> (!rawType.isEnum()) {</div>
<div class="line">      <span class="keywordflow">return</span> null;</div>
<div class="line">    }</div>
<div class="line"></div>
<div class="line">    <span class="keyword">final</span> Map&lt;String, T&gt; lowercaseToConstant = <span class="keyword">new</span> HashMap&lt;String, T&gt;();</div>
<div class="line">    <span class="keywordflow">for</span> (T constant : rawType.getEnumConstants()) {</div>
<div class="line">      lowercaseToConstant.put(toLowercase(constant), constant);</div>
<div class="line">    }</div>
<div class="line"></div>
<div class="line">    <span class="keywordflow">return</span> <span class="keyword">new</span> TypeAdapter&lt;T&gt;() {</div>
<div class="line">      <span class="keyword">public</span> <span class="keywordtype">void</span> write(JsonWriter out, T value) <span class="keywordflow">throws</span> IOException {</div>
<div class="line">        <span class="keywordflow">if</span> (value == null) {</div>
<div class="line">          out.nullValue();</div>
<div class="line">        } <span class="keywordflow">else</span> {</div>
<div class="line">          out.value(toLowercase(value));</div>
<div class="line">        }</div>
<div class="line">      }</div>
<div class="line"></div>
<div class="line">      <span class="keyword">public</span> T read(JsonReader reader) <span class="keywordflow">throws</span> IOException {</div>
<div class="line">        <span class="keywordflow">if</span> (reader.peek() == JsonToken.NULL) {</div>
<div class="line">          reader.nextNull();</div>
<div class="line">          <span class="keywordflow">return</span> null;</div>
<div class="line">        } <span class="keywordflow">else</span> {</div>
<div class="line">          <span class="keywordflow">return</span> lowercaseToConstant.get(reader.nextString());</div>
<div class="line">        }</div>
<div class="line">      }</div>
<div class="line">    };</div>
<div class="line">  }</div>
<div class="line"></div>
<div class="line">  <span class="keyword">private</span> String toLowercase(Object o) {</div>
<div class="line">    <span class="keywordflow">return</span> o.toString().toLowerCase(Locale.US);</div>
<div class="line">  }</div>
<div class="line">}</div>
</div><!-- fragment --> </pre><p>Type adapter factories select which types they provide type adapters for. If a factory cannot support a given type, it must return null when that type is passed to <a class="el" href="interfacecom_1_1google_1_1gson_1_1_type_adapter_factory.html#a8f4c55ec13b384a3e1cba3c3a827ee48">create</a>. Factories should expect</p><div class="fragment"><div class="line"><a class="code" href="interfacecom_1_1google_1_1gson_1_1_type_adapter_factory.html#a8f4c55ec13b384a3e1cba3c3a827ee48">create</a>() </div>
</div><!-- fragment --><p> to be called on them for many types and should return null for most of those types. In the above example the factory returns null for calls to</p><div class="fragment"><div class="line"><a class="code" href="interfacecom_1_1google_1_1gson_1_1_type_adapter_factory.html#a8f4c55ec13b384a3e1cba3c3a827ee48">create</a>() </div>
</div><!-- fragment --><p> where</p><div class="fragment"><div class="line">type </div>
</div><!-- fragment --><p> is not an enum.</p>
<p>A factory is typically called once per type, but the returned type adapter may be used many times. It is most efficient to do expensive work like reflection in</p><div class="fragment"><div class="line"><a class="code" href="interfacecom_1_1google_1_1gson_1_1_type_adapter_factory.html#a8f4c55ec13b384a3e1cba3c3a827ee48">create</a>() </div>
</div><!-- fragment --><p> so that the type adapter's</p><div class="fragment"><div class="line">read() </div>
</div><!-- fragment --><p> and</p><div class="fragment"><div class="line">write() </div>
</div><!-- fragment --><p> methods can be very fast. In this example the mapping from lowercase name to enum value is computed eagerly.</p>
<p>As with type adapters, factories must be <em>registered</em> with a <a class="el" href="classcom_1_1google_1_1gson_1_1_gson_builder.html">com.google.gson.GsonBuilder</a> for them to take effect: </p><pre><div class="fragment"><div class="line">GsonBuilder builder = <span class="keyword">new</span> GsonBuilder();</div>
<div class="line">builder.registerTypeAdapterFactory(<span class="keyword">new</span> LowercaseEnumTypeAdapterFactory());</div>
<div class="line">...</div>
<div class="line">Gson gson = builder.create();</div>
</div><!-- fragment --> </pre><p> If multiple factories support the same type, the factory registered earlier takes precedence.</p>
<h3>Example: composing other type adapters</h3>
<p>In this example we implement a factory for Guava's</p><div class="fragment"><div class="line">Multiset </div>
</div><!-- fragment --><p> collection type. The factory can be used to create type adapters for multisets of any element type: the type adapter for</p><div class="fragment"><div class="line">Multiset&lt;String&gt; </div>
</div><!-- fragment --><p> is different from the type adapter for</p><div class="fragment"><div class="line">Multiset&lt;URL&gt; </div>
</div><!-- fragment --><p> .</p>
<p>The type adapter <em>delegates</em> to another type adapter for the multiset elements. It figures out the element type by reflecting on the multiset's type token. A</p><div class="fragment"><div class="line">Gson </div>
</div><!-- fragment --><p> is passed in to</p><div class="fragment"><div class="line"><a class="code" href="interfacecom_1_1google_1_1gson_1_1_type_adapter_factory.html#a8f4c55ec13b384a3e1cba3c3a827ee48">create</a> </div>
</div><!-- fragment --><p> for just this purpose: </p><pre><div class="fragment"><div class="line"><span class="keyword">public</span> <span class="keyword">class </span>MultisetTypeAdapterFactory <span class="keyword">implements</span> TypeAdapter.Factory {</div>
<div class="line">  public &lt;T&gt; TypeAdapter&lt;T&gt; <a class="code" href="interfacecom_1_1google_1_1gson_1_1_type_adapter_factory.html#a8f4c55ec13b384a3e1cba3c3a827ee48">create</a>(Gson gson, TypeToken&lt;T&gt; typeToken) {</div>
<div class="line">    Type type = typeToken.getType();</div>
<div class="line">    <span class="keywordflow">if</span> (typeToken.getRawType() != Multiset.class</div>
<div class="line">        || !(type instanceof ParameterizedType)) {</div>
<div class="line">      <span class="keywordflow">return</span> null;</div>
<div class="line">    }</div>
<div class="line"></div>
<div class="line">    Type elementType = ((ParameterizedType) type).getActualTypeArguments()[0];</div>
<div class="line">    TypeAdapter&lt;?&gt; elementAdapter = gson.getAdapter(TypeToken.get(elementType));</div>
<div class="line">    <span class="keywordflow">return</span> (TypeAdapter&lt;T&gt;) newMultisetAdapter(elementAdapter);</div>
<div class="line">  }</div>
<div class="line"></div>
<div class="line">  private &lt;E&gt; TypeAdapter&lt;Multiset&lt;E&gt;&gt; newMultisetAdapter(</div>
<div class="line">      <span class="keyword">final</span> TypeAdapter&lt;E&gt; elementAdapter) {</div>
<div class="line">    <span class="keywordflow">return</span> <span class="keyword">new</span> TypeAdapter&lt;Multiset&lt;E&gt;&gt;() {</div>
<div class="line">      <span class="keyword">public</span> <span class="keywordtype">void</span> write(JsonWriter out, Multiset&lt;E&gt; value) <span class="keywordflow">throws</span> IOException {</div>
<div class="line">        <span class="keywordflow">if</span> (value == null) {</div>
<div class="line">          out.nullValue();</div>
<div class="line">          <span class="keywordflow">return</span>;</div>
<div class="line">        }</div>
<div class="line"></div>
<div class="line">        out.beginArray();</div>
<div class="line">        <span class="keywordflow">for</span> (Multiset.Entry&lt;E&gt; entry : value.entrySet()) {</div>
<div class="line">          out.value(entry.getCount());</div>
<div class="line">          elementAdapter.write(out, entry.getElement());</div>
<div class="line">        }</div>
<div class="line">        out.endArray();</div>
<div class="line">      }</div>
<div class="line"></div>
<div class="line">      <span class="keyword">public</span> Multiset&lt;E&gt; read(JsonReader in) <span class="keywordflow">throws</span> IOException {</div>
<div class="line">        <span class="keywordflow">if</span> (in.peek() == JsonToken.NULL) {</div>
<div class="line">          in.nextNull();</div>
<div class="line">          <span class="keywordflow">return</span> null;</div>
<div class="line">        }</div>
<div class="line"></div>
<div class="line">        Multiset&lt;E&gt; result = LinkedHashMultiset.create();</div>
<div class="line">        in.beginArray();</div>
<div class="line">        <span class="keywordflow">while</span> (in.hasNext()) {</div>
<div class="line">          <span class="keywordtype">int</span> count = in.nextInt();</div>
<div class="line">          E element = elementAdapter.read(in);</div>
<div class="line">          result.add(element, count);</div>
<div class="line">        }</div>
<div class="line">        in.endArray();</div>
<div class="line">        <span class="keywordflow">return</span> result;</div>
<div class="line">      }</div>
<div class="line">    };</div>
<div class="line">  }</div>
<div class="line">}</div>
</div><!-- fragment --> </pre><p> Delegating from one type adapter to another is extremely powerful; it's the foundation of how <a class="el" href="classcom_1_1google_1_1gson_1_1_gson.html">Gson</a> converts Java objects and collections. Whenever possible your factory should retrieve its delegate type adapter in the </p><div class="fragment"><div class="line"><a class="code" href="interfacecom_1_1google_1_1gson_1_1_type_adapter_factory.html#a8f4c55ec13b384a3e1cba3c3a827ee48">create</a>() </div>
</div><!-- fragment --><p> method; this ensures potentially-expensive type adapter creation happens only once.</p>
<dl class="section since"><dt>自从</dt><dd>2.1 </dd></dl>
</div><h2 class="groupheader">成员函数说明</h2>
<a class="anchor" id="a8f4c55ec13b384a3e1cba3c3a827ee48"></a>
<div class="memitem">
<div class="memproto">
      <table class="memname">
        <tr>
          <td class="memname">&lt;T&gt; TypeAdapter&lt;T&gt; com.google.gson.TypeAdapterFactory.create </td>
          <td>(</td>
          <td class="paramtype"><a class="el" href="classcom_1_1google_1_1gson_1_1_gson.html">Gson</a>&#160;</td>
          <td class="paramname"><em>gson</em>, </td>
        </tr>
        <tr>
          <td class="paramkey"></td>
          <td></td>
          <td class="paramtype">TypeToken&lt; T &gt;&#160;</td>
          <td class="paramname"><em>type</em>&#160;</td>
        </tr>
        <tr>
          <td></td>
          <td>)</td>
          <td></td><td></td>
        </tr>
      </table>
</div><div class="memdoc">
<p>Returns a type adapter for</p><div class="fragment"><div class="line">type </div>
</div><!-- fragment --><p> , or null if this factory doesn't support</p><div class="fragment"><div class="line">type </div>
</div><!-- fragment --><p> . </p>

<p>在 <a class="el" href="classcom_1_1google_1_1gson_1_1internal_1_1bind_1_1_map_type_adapter_factory.html#a64975b8a0585ae29803d258f39c93346">com.google.gson.internal.bind.MapTypeAdapterFactory</a> , 以及 <a class="el" href="classcom_1_1google_1_1gson_1_1internal_1_1bind_1_1_collection_type_adapter_factory.html#ae2bc98924e6e777ebdcf03377b0acfe9">com.google.gson.internal.bind.CollectionTypeAdapterFactory</a> 内被实现.</p>

</div>
</div>
<hr/>该接口的文档由以下文件生成:<ul>
<li>src/com/google/gson/<a class="el" href="_type_adapter_factory_8java.html">TypeAdapterFactory.java</a></li>
</ul>
</div><!-- contents -->
<!-- start footer part -->
<hr class="footer"/><address class="footer"><small>
生成于 2014年 十一月 6日 星期四 11:22:04 , 为 AndBase开发框架使用  &#160;<a href="http://www.doxygen.org/index.html">
<img class="footer" src="doxygen.png" alt="doxygen"/>
</a> 1.8.8
</small></address>
</body>
</html>
